package com.kun.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.kun.music.domain.Comment;
import com.kun.music.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author fangkun
 * @create 2023-01-04-15:28
 */
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    //歌曲评论
    @GetMapping("/comment/song/detail")
    public Object getSongComment(@RequestParam("songId") int songId){
        return commentService.getSongComment(songId);
    }

    //歌单评论
    @GetMapping("/comment/songList/detail")
    public Object getCommentOfSongListId(@RequestParam("songListId") int id){
        return commentService.getCommentOfSongListId(id);
    }

    @PostMapping("/comment/add")
    public Object addComment(HttpServletRequest request){
        JSONObject jsonObject = new JSONObject();

        String songListId = request.getParameter("songListId");
        String userId = request.getParameter("userId");
        String type = request.getParameter("type");
        String content = request.getParameter("content");

        Comment comment = new Comment();
        comment.setSongListId(Integer.valueOf(songListId));
        comment.setUserId(Integer.valueOf(userId));
        comment.setType(new Byte(type));
        comment.setContent(content);
        comment.setCreateTime(new Date());

        boolean res = commentService.addComment(comment);

        if (res){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "评论成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "评论失败");
            return jsonObject;
        }
    }

    @PostMapping(value = "/comment/like")
    public Object commentOfLike(HttpServletRequest req){

        JSONObject jsonObject = new JSONObject();
        String id = req.getParameter("id").trim();
        String up = req.getParameter("up").trim();

        Comment comment = new Comment();
        comment.setId(Integer.parseInt(id));
        comment.setUp(Integer.parseInt(up));
        boolean res = commentService.updateCommentMsg(comment);
        if (res){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "点赞成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "点赞失败");
            return jsonObject;
        }
    }
}
